﻿using BW.WMS.Domain.Entities;
using BW.WMS.Infrastructure.Data.Contexts;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static BW.WMS.Infrastructure.Data.Contexts.DBContext;
using MySql.Data.MySqlClient;

namespace BW.WMS.Infrastructure.Data.Repositories
{
    /// <summary>
    /// 仓储实现
    /// </summary>
    public class DataRepository<T>
    {
        private readonly DBContext db;

        public DataRepository(DBContext db)
        {
            this.db = db;
        }

        public T Get(int id,string sql)
        {
            return db.GetConn().QueryFirstOrDefault<T>(sql, new { Id = id });
        }

        public List<T> GetList(string sql)
        {
            return db.GetConn().Query<T>(sql).ToList();
        }

        public int Add(T t,string sql)
        {
            return db.GetConn().QuerySingle<int>(sql, t);
        }

        public bool Update(T t,string sql)
        {
            int result = db.GetConn().Execute(sql, t);
            return result > 0;
        }
        public bool Delete(int id,string sql)
        {
            int result = db.GetConn().Execute(sql, new { Id = id });
            return result > 0;
        }

    }
}
